import 'package:flutter/material.dart';
import 'package:znmm/config/app_color.dart';

//单选框
class GestureCheckbox extends StatefulWidget {
  final bool initialValue;
  final ValueChanged<bool>? onChanged;
  final String strDef;
  final double fWidth;
  final double fHeight;

  const GestureCheckbox(
      {Key? key,
      this.initialValue = false,
      this.onChanged,
      this.strDef = '',
      this.fHeight = 50,
      this.fWidth = 120})
      : super(key: key);

  @override
  // ignore: library_private_types_in_public_api
  _GestureCheckboxState createState() => _GestureCheckboxState();
}

class _GestureCheckboxState extends State<GestureCheckbox> {
  late bool _isChecked;

  @override
  void initState() {
    super.initState();
    _isChecked = widget.initialValue;
  }

  @override
  void didUpdateWidget(GestureCheckbox oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget.initialValue != oldWidget.initialValue) {
      _isChecked = widget.initialValue;
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: () {
          setState(() {
            _isChecked = !_isChecked;
          });
          if (widget.onChanged != null) {
            widget.onChanged!(_isChecked);
          }
        },
        child: Container(
          padding: const EdgeInsets.fromLTRB(5, 2, 5, 2),
          constraints: const BoxConstraints(minHeight: 50), // 设置最小高度为56
          height: widget.fHeight,
          width: widget.fWidth,
          // decoration: BoxDecoration(
          //   border: Border.all(color: Colors.grey, width: 0.5), // 设置边框颜色和宽度
          //   borderRadius: BorderRadius.circular(5.0),
          // ),
          child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              mainAxisSize: MainAxisSize.max,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Container(
                  width: 20.0,
                  height: 20.0,
                  decoration: BoxDecoration(
                    border: Border.all(
                      color: Colors.grey,
                      width: 1.0,
                    ),
                    color:
                        _isChecked ? AppColor.blue396EC6 : Colors.transparent,
                  ),
                  child: _isChecked
                      ? Transform.translate(
                          offset: const Offset(-2, -2),
                          child: const Icon(Icons.check,
                              color: Colors.white, size: 22.0),
                        )
                      : null,
                ),
                const SizedBox(
                  width: 10,
                ),
                widget.strDef.isNotEmpty
                    ? Expanded(
                        child: Text(
                        widget.strDef,
                        style: TextStyle(
                            fontSize: 17,
                            color:
                                _isChecked ? AppColor.blue396EC6 : Colors.grey),
                      ))
                    : const SizedBox()
              ]),
        ));
  }
}
